//
//  HomeSelectionHeadView.m
//  GiftNote
//
//  Created by lanou on 15/8/12.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#define kHomeBeatyGift @"http://api.liwushuo.com/v1/collections/22/posts?gender=1&generation=1&limit=20&offset="

#import <QuartzCore/QuartzCore.h>
#import "HomeSelectionHeadView.h"
#import "HomeHeader.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "HomeWheelModel.h"
#import "HomeBeautyModel.h"
#import "UIColor+HexString.h"
#import "HomeBaseViewController.h"
#import "HomeStrategyViewController.h"

@interface HomeSelectionHeadView ()<UIScrollViewDelegate>

@property (nonatomic, strong)NSMutableArray *beautyArray;  //存储轮播图下方按钮的数组
@property (nonatomic, strong)NSMutableArray *beautyLabelArray;


@property (nonatomic, strong)CATransition *animation;
@property (nonatomic, assign)NSInteger lastOffset;
@property (nonatomic, strong)HomeBaseViewController *baseVC;
@property (nonatomic, strong)HomeStrategyViewController *strategyVC;

@end

@implementation HomeSelectionHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        _animation = [CATransition animation];
        
        _beautyArray = [NSMutableArray array];
        _beautyLabelArray = [NSMutableArray array];
        
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight / 4)];
        
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        [self addSubview:_scrollView];
        
        
        
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, kHomeHeight / 4 - 30, kHomeWidth, 30)];
        _pageControl.numberOfPages = 6;
        [self addSubview:_pageControl];
        
        for (int i = 0; i < 4; i++) {
            _beautyButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _beautyButton.frame = CGRectMake(kHomeWidth / 20 + (kHomeWidth * 5 / 20) * i, CGRectGetMaxY(_scrollView.frame) + kHomeWidth / 20, kHomeWidth * 3 / 20, kHomeWidth * 3 / 20);
            [_beautyArray addObject:_beautyButton];
            [self addSubview:_beautyButton];
            
            
            _beautyLabel = [[UILabel alloc]initWithFrame:CGRectMake(kHomeWidth / 20 + (kHomeWidth * 5 / 20) * i, CGRectGetMaxY(_beautyButton.frame), kHomeWidth * 3 / 20, 15)];
            _beautyLabel.textAlignment = NSTextAlignmentLeft;
            _beautyLabel.font = [UIFont systemFontOfSize:14];
            [_beautyLabelArray addObject:_beautyLabel];
            [self addSubview:_beautyLabel];
        }
        
    }
    return self;
}

- (void)setHeadScrollArray:(NSArray *)headScrollArray
{
    _headScrollArray = headScrollArray;
    
    _scrollView.contentSize = CGSizeMake(kHomeWidth * (_headScrollArray.count + 1), kHomeHeight / 4);
    _scrollView.contentOffset = CGPointMake(kHomeWidth, 0);
    
    HomeWheelModel *firstWheelModel = [_headScrollArray lastObject];
    UIImageView *firstImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight / 4)];;
    [firstImageView sd_setImageWithURL:[NSURL URLWithString:firstWheelModel.image_url] placeholderImage:nil];
    [_scrollView addSubview:firstImageView];
    
    for (int i = 0; i < _headScrollArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kHomeWidth * (i + 1), 0, kHomeWidth, kHomeHeight / 4)];
        HomeWheelModel *wheelModel = _headScrollArray[i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:wheelModel.image_url] placeholderImage:nil];
        [_scrollView addSubview:imageView];
    }
}

- (void)setHeadBeautyArray:(NSArray *)headBeautyArray
{
    _headBeautyArray = headBeautyArray;
    for (int i = 0; i < _headBeautyArray.count; i++) {
        HomeBeautyModel *beautyModel = _headBeautyArray[i];
        UIButton *button = _beautyArray[i];
        UILabel *label = _beautyLabelArray[i];
        [button sd_setBackgroundImageWithURL:[NSURL URLWithString:beautyModel.icon_url] forState:UIControlStateNormal placeholderImage:nil];
        button.tag = i * 100;
        [button addTarget:self action:@selector(doTapBeautyAction:) forControlEvents:UIControlEventTouchUpInside];
        label.text = beautyModel.title;
        label.textColor = [UIColor colorWithHexString:beautyModel.color];
    }
    
}


- (void)doTapBeautyAction:(UIButton *)sender
{
    switch (sender.tag) {
        case 0:
        {
            _baseVC = [[HomeBaseViewController alloc]init];
            _baseVC.url = kHomeBeatyGift;
            _baseVC.hidesBottomBarWhenPushed = YES;
            _baseVC.navigationItem.title = @"每日十件小礼物";
            [_naVC pushViewController:_baseVC animated:YES];
        }
            break;
            
        case 100:
        {
            _strategyVC = [[HomeStrategyViewController alloc]init];
            _strategyVC.hidesBottomBarWhenPushed = YES;
            [_naVC pushViewController:_strategyVC animated:YES];
        }
            break;
            
        case 200:
        {
            
        }
            break;
            
        case 300:
        {
            
        }
            break;
            
        default:
            break;
    }
}


#pragma mark - UIScrollView

//滑动过程中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (_scrollView.contentOffset.x < _lastOffset) {
        _animation.subtype = kCATransitionFromLeft;
        _scrollView.contentOffset = CGPointMake(_lastOffset - kHomeWidth, 0);
    } else if (_scrollView.contentOffset.x > _lastOffset) {
        _animation.subtype = kCATransitionFromRight;
        _scrollView.contentOffset = CGPointMake(_lastOffset + kHomeWidth, 0);
    }
    
    
    if (_scrollView.contentOffset.x < 0) {
        _scrollView.contentOffset = CGPointMake(kHomeWidth * _headScrollArray.count, 0);
    } else if (_scrollView.contentOffset.x > _headScrollArray.count * kHomeWidth) {
        _scrollView.contentOffset = CGPointMake(0, 0);
    }
    
}

//将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    
}

//将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _animation.delegate = self;
    _animation.duration = 0.7;
    _animation.timingFunction = UIViewAnimationCurveEaseInOut;
    _animation.type = @"cube";
    [[_scrollView layer]addAnimation:_animation forKey:@"animation"];

    _lastOffset = scrollView.contentOffset.x;
    
}

//结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
}

//结束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    
}

@end
